//
//  DKVistorView.swift
//  Sina
//
//  Created by 赵柏清 on 16/7/29.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit
import SnapKit

protocol DKVistorViewDelegate : NSObjectProtocol {
    
    func didRegisterMethod()
    
}

class DKVistorView: UIView {
    
    // 设置闭包,方便回调
    var closure : ()->() = {()->()in}
    
    // 设置代理属性
    weak var delegate : DKVistorViewDelegate?
    
    // MARK: 外界设置的方法
    func setVistorViewInfo(Massage: String,backgroundImage: String,isShowTurn: Bool = false){

        turnImageView.hidden = !isShowTurn
        
        backgroundImageView.image = UIImage(named: backgroundImage)
        
        tipsLabel.text = Massage
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        startAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("please implement this method")
    }
    
    // 添加子控件
    private func setupUI(){
        // 设置背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(backgroundImageView)
        addSubview(tipsLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
    }
    
    override func layoutSubviews() {
        
        turnImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        backgroundImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        tipsLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipsLabel.snp_left)
            make.top.equalTo(tipsLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipsLabel.snp_right)
            make.top.equalTo(tipsLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }

        
        
    }
    // 注册按钮的点击事件
    @objc private func registerBtnClick(){
        
        // 注册跳转
        delegate?.didRegisterMethod()
        
    }
    
    // 登录按钮的点击事件
    @objc private func loginBtnClick(){
        
        closure()
        
    }
    
    // MARK: 视图上控件的懒加载
    /**
    *  转动图片
    */
    lazy var turnImageView : UIImageView = {
        
        let turnImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return turnImage
        
    }()
    
    ///蒙版
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
    /// 背景图片
    lazy var backgroundImageView : UIImageView = {
        
        let backgroudImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return backgroudImage
    }()
    /// 提示文字
    lazy var tipsLabel : UILabel = {
        
        let tips = UILabel()
        
        tips.textAlignment = .Center
        tips.font = UIFont.systemFontOfSize(14)
        tips.numberOfLines = 0
        
        return tips
        
    }()
    /// 注册按钮
    lazy var registerBtn : UIButton = {
        let registerButton = UIButton()
        // 按钮的图片
    
        let buttonImage = UIImage(named: "common_button_white_disable")
        // 代码切割按钮的图片
        buttonImage?.stretchableImageWithLeftCapWidth(Int(buttonImage!.size.width * CGFloat(0.5)), topCapHeight: Int(buttonImage!.size.height * CGFloat(0.5)))
        
        // 设置按钮
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerButton.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
        
        registerButton.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return registerButton
    }()
    /// 登录按钮
    lazy var loginBtn : UIButton = {
        
        let login = UIButton()
        
        let buttonImage = UIImage(named: "common_button_white_disable")
        // 代码切割按钮的图片
        buttonImage?.stretchableImageWithLeftCapWidth(Int(buttonImage!.size.width * CGFloat(0.5)), topCapHeight: 0)
        
        login.setTitle("登录", forState: UIControlState.Normal)
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        login.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
        
        login.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
    // MARK: 添加旋转的核心动画
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        animation.removedOnCompletion = false
        turnImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
}

extension DKVistorView {

}
